<template>
  <div class="app-container">
    <el-tabs v-model="activeTab">
      <el-tab-pane label="评语管理" name="comment">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="评语内容" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入评语内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入排序值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manage:comment:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:comment:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:comment:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="commentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="commentId" width="100" />
      <el-table-column label="评语内容" align="center" prop="content" />
      <el-table-column label="排序" align="center" prop="orderNum" width="120" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:comment:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manage:comment:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="评语内容" prop="content">
          <el-input type="textarea" :rows="4" v-model="form.content" placeholder="请输入评语内容" />
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input-number v-model="form.orderNum" :min="0" :step="1" :max="9999" controls-position="right" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
      </el-tab-pane>

      <el-tab-pane label="激励语句" name="motivation">
        <el-form :model="motivationQuery" ref="motivationQueryForm" size="small" :inline="true" v-show="showMotivationSearch" label-width="80px">
          <el-form-item label="激励内容" prop="content">
            <el-input
              v-model="motivationQuery.content"
              placeholder="请输入激励语句"
              clearable
              @keyup.enter.native="handleMotivationQuery"
            />
          </el-form-item>
          <el-form-item label="排序" prop="orderNum">
            <el-input
              v-model="motivationQuery.orderNum"
              placeholder="请输入排序值"
              clearable
              @keyup.enter.native="handleMotivationQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleMotivationQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetMotivationQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleMotivationAdd"
              v-hasPermi="['manage:motivation:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="motivationSingle"
              @click="handleMotivationUpdate"
              v-hasPermi="['manage:motivation:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="motivationMultiple"
              @click="handleMotivationDelete"
              v-hasPermi="['manage:motivation:remove']"
            >删除</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showMotivationSearch" @queryTable="getMotivationList"></right-toolbar>
        </el-row>

        <el-table v-loading="motivationLoading" :data="motivationList" @selection-change="handleMotivationSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="ID" align="center" prop="motivationId" width="100" />
          <el-table-column label="激励内容" align="center" prop="content" />
          <el-table-column label="排序" align="center" prop="orderNum" width="120" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleMotivationUpdate(scope.row)"
                v-hasPermi="['manage:motivation:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleMotivationDelete(scope.row)"
                v-hasPermi="['manage:motivation:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="motivationTotal>0"
          :total="motivationTotal"
          :page.sync="motivationQuery.pageNum"
          :limit.sync="motivationQuery.pageSize"
          @pagination="getMotivationList"
        />

        <el-dialog :title="motivationTitle" :visible.sync="motivationOpen" width="500px" append-to-body>
          <el-form ref="motivationForm" :model="motivationForm" :rules="motivationRules" label-width="80px">
            <el-form-item label="激励内容" prop="content">
              <el-input type="textarea" :rows="4" v-model="motivationForm.content" placeholder="请输入激励语句" />
            </el-form-item>
            <el-form-item label="排序" prop="orderNum">
              <el-input-number v-model="motivationForm.orderNum" :min="0" :step="1" :max="9999" controls-position="right" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitMotivationForm">确 定</el-button>
            <el-button @click="cancelMotivation">取 消</el-button>
          </div>
        </el-dialog>
      </el-tab-pane>
    </el-tabs>
  </div>
  </template>

<script>
import { listComment, getComment, addComment, updateComment, delComment } from '@/api/manage/comment'
import { listMotivation, getMotivation, addMotivation, updateMotivation, delMotivation } from '@/api/manage/motivation'

export default {
  name: 'Comment',
  data() {
    return {
      activeTab: 'comment',
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      commentList: [],
      title: '',
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        content: null,
        orderNum: null
      },
      form: {},
      rules: {
        content: [
          { required: true, message: '评语内容不能为空', trigger: 'blur' }
        ],
        orderNum: [
          { required: true, message: '排序不能为空', trigger: 'change' }
        ]
      },
      // Motivation module state
      motivationLoading: true,
      motivationIds: [],
      motivationSingle: true,
      motivationMultiple: true,
      showMotivationSearch: true,
      motivationTotal: 0,
      motivationList: [],
      motivationTitle: '',
      motivationOpen: false,
      motivationQuery: {
        pageNum: 1,
        pageSize: 10,
        content: null,
        orderNum: null
      },
      motivationForm: {},
      motivationRules: {
        content: [
          { required: true, message: '激励内容不能为空', trigger: 'blur' }
        ],
        orderNum: [
          { required: true, message: '排序不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getMotivationList()
  },
  methods: {
    getList() {
      this.loading = true
      listComment(this.queryParams).then(response => {
        this.commentList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        commentId: null,
        content: null,
        orderNum: null
      }
      this.resetForm('form')
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.commentId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加评语'
    },
    handleUpdate(row) {
      this.reset()
      const commentId = row.commentId || this.ids
      getComment(commentId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改评语'
      })
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.commentId != null) {
            updateComment(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addComment(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    handleDelete(row) {
      const commentIds = row.commentId || this.ids
      this.$modal.confirm('是否确认删除评语编号为"' + commentIds + '"的数据项？').then(function() {
        return delComment(commentIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    handleExport() {
      this.download('manage/comment/export', { ...this.queryParams }, `comment_${new Date().getTime()}.xlsx`)
    },
    // Motivation methods
    getMotivationList() {
      this.motivationLoading = true
      listMotivation(this.motivationQuery).then(response => {
        this.motivationList = response.rows
        this.motivationTotal = response.total
        this.motivationLoading = false
      })
    },
    cancelMotivation() {
      this.motivationOpen = false
      this.resetMotivation()
    },
    resetMotivation() {
      this.motivationForm = {
        motivationId: null,
        content: null,
        orderNum: null
      }
      this.resetForm('motivationForm')
    },
    handleMotivationQuery() {
      this.motivationQuery.pageNum = 1
      this.getMotivationList()
    },
    resetMotivationQuery() {
      this.resetForm('motivationQueryForm')
      this.handleMotivationQuery()
    },
    handleMotivationSelectionChange(selection) {
      this.motivationIds = selection.map(item => item.motivationId)
      this.motivationSingle = selection.length!==1
      this.motivationMultiple = !selection.length
    },
    handleMotivationAdd() {
      this.resetMotivation()
      this.motivationOpen = true
      this.motivationTitle = '添加激励语句'
    },
    handleMotivationUpdate(row) {
      this.resetMotivation()
      const motivationId = row.motivationId || this.motivationIds
      getMotivation(motivationId).then(response => {
        this.motivationForm = response.data
        this.motivationOpen = true
        this.motivationTitle = '修改激励语句'
      })
    },
    submitMotivationForm() {
      this.$refs['motivationForm'].validate(valid => {
        if (valid) {
          if (this.motivationForm.motivationId != null) {
            updateMotivation(this.motivationForm).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.motivationOpen = false
              this.getMotivationList()
            })
          } else {
            addMotivation(this.motivationForm).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.motivationOpen = false
              this.getMotivationList()
            })
          }
        }
      })
    },
    handleMotivationDelete(row) {
      const motivationIds = row.motivationId || this.motivationIds
      this.$modal.confirm('是否确认删除激励语句编号为"' + motivationIds + '"的数据项？').then(function() {
        return delMotivation(motivationIds)
      }).then(() => {
        this.getMotivationList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    handleMotivationExport() {
      this.download('manage/motivation/export', { ...this.motivationQuery }, `motivation_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>